<?php
/**
 * [CLOUD]版权所有，保留所有权利
 * This is not a freeware, use is subject to license terms
 *
 * $Id：upload.class.php
 */
class Upload
{
	public $attach = array();
	
	private $attackMaxSize;
	
	private static $object = null;
	
	public function __construct()
	{
		$this->attackMaxSize = Common::getConfig( 'System' , 'attackMaxSize' );
	}
	
	/**
	 * 初始化附件信息
	 * @param string $attach
	 * @param string $dir
	 * @param string $name
	 */
	private function init( $attach , $dir , $name = '' )
	{
        if ( empty( $attach ) || !is_array( $attach ) || !$this -> is_upload_file( $attach['tmp_name'] ) || empty($attach['name']) 
        	|| empty( $attach['size'] ) )
        {
            $this->attach = array();
            return false;
        }
        else 
        {
        	$this->attach = array();
            $attach['size'] = intval( $attach['size'] );
            $attach['filename'] = ( strtolower( CHARSET ) != 'utf-8' ) ? iconv( 'gbk' , 'utf-8' , $attach['name'] ) : $attach['name'];
            $attach['attach_maxsize'] = $this->attackMaxSize ? $this->attackMaxSize * 1024 * 1024 : 2048 * 1024; 
            $attach['ext'] = $this->get_ext( $attach['name'] );
            $attach['save_path'] = $this->set_path( ROOT_DIR . ATTACHMENT . ( $dir ? $dir . '/' : '' ) . date( "Y" ) . '/' . date("md") );
            if ( $name )
            {
                $attach['save_name'] = $name;
            }
            else
            {
                $attach['save_name'] = $this -> create_tempname();
            }
            $attach['relative_path'] = ( $dir ? $dir . '/' : '' ) . date( "Y" ) . '/' . date("md") . '/' . $attach['save_name'] . '.' . $attach['ext'];
            $attach['attach_path'] = $attach['save_path'] . $attach['save_name'] . '.' . $attach['ext'];
            $this->attach = $attach;
        }
	}
	
	/**
	 * 获取上传附件信息
	 */
	public function getAttach()
	{
		return $this->attach;
	}
	
	/**
	 * 获取附件绝对路径
	 */
	public function getRelativePath()
	{
		return $this->attach['relative_path'];
	}
	
	/**
	 * 获取附件相对路径
	 */
	public function getAttachPath()
	{
		return $this->attach['attach_path'];
	}
	
	/**
	 * 上传附件
	 * @param string $attach
	 * @param string $dir
	 * @param sring $name
	 */
	public function uploadFile( $attach , $dir = '' , $name = '' )
	{
		$this->init( $attach , $dir , $name );
		if ( $this->attach['error'] !== 0 )
		{
			throw new BaseException( BaseException::USER_ERROR_UPLOAD );
		}
		
		if ( $this->attach['size'] > 0 && $this->attach['size'] > $this->attach['attach_maxsize'] )
		{
			throw new BaseException( BaseException::USER_ERROR_ATTACHMENT_LARGE );
		}
		if ( !$this->check_file_ext() )
		{
			throw new BaseException( BaseException::USER_ERROR_NOT_ALLOWED_TYPE );
		}
		if ( !move_uploaded_file( $this->attach['tmp_name'] , $this->attach['attach_path'] ) )
		{
			throw new BaseException( BaseException::USER_ERROR_UPLOAD );
		}
		return true;
	}
	
	/**
	 * 验证是否上传
	 * @param string $tem_file
	 */
	private function is_upload_file( $tem_file )
	{
		return $tem_file && is_uploaded_file( $tem_file );
	}
	
	/**
	 * 获取扩展名
	 * @param string $name
	 */
	public function get_ext( $name )
	{
		return Common::deep_addslashes( strtolower( substr( strrchr( $name , '.' ) , 1 ) ) );
	}
	
	/**
	 * 生成临时上传文件名
	 * @return string
	 */
    public function create_tempname()
    {
    	$time_arr = explode( ' ', microtime() );
    	$microtime = ( string )( $time_arr[1] + $time_arr[0] );
    	$microtime = str_replace( '.' , '' , $microtime );
        return $microtime . mt_rand( 0 , 9999 );
    }
	
    /**
     * 检验上传文件扩展名
     */
    public function check_file_ext()
    {
    	$allow_file_ext = array( 'jpg', 'jpeg', 'gif', 'png', 'bmp' );
    	if ( $this->attach['ext'] && in_array($this->attach['ext'] , $allow_file_ext ) )
    	{
    	   return true;
    	}
    	else
    	{ 
    	   return false;
    	}
    }
    
    /**
     * 设置路径
     * @param string $path
     */
    private function set_path( $path )
    {
    	$path  = str_replace( '\\', '/', $path );
    	$path = substr( $path, -1 ) != '/' ? $path . '/' : $path;
    	if ( !file_exists( $path ) )
    	{
    		Dir::deepMakeDir( $path );
    	}
    	return $path;
    }
	
}